﻿// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "BToolkit/AlphaByGrayTextureUGUI" { 
    Properties { 
        _MainTex ("MainTex (RGB)", 2D) = "white" {} 
        _Mask ("Mask (RGB)", 2D) = "white" {} 

        //Mask Start
	    _StencilComp("Stencil Comparison", Float) = 8
	    _Stencil("Stencil ID", Float) = 0
	    _StencilOp("Stencil Operation", Float) = 0
	    _StencilWriteMask("Stencil Write Mask", Float) = 255
	    _StencilReadMask("Stencil Read Mask", Float) = 255
	    _ColorMask("Color Mask", Float) = 15
	    //Mask End
    } 
    SubShader { 
        Tags { "RenderType"="Opaque" } 
        Blend SrcAlpha OneMinusSrcAlpha 

        //Mask Start
	    Stencil
	    {
	       Ref[_Stencil]
	       Comp[_StencilComp]
	       Pass[_StencilOp]
	       ReadMask[_StencilReadMask]
	       WriteMask[_StencilWriteMask]
	    }
	    ColorMask[_ColorMask]
	    //Mask End

        pass 
        { 
            CGPROGRAM 

            #pragma vertex vertext_convert
            #pragma fragment fragment_convert
            #include "UnityCG.cginc" 

            sampler2D  _MainTex; 
            sampler2D  _Mask; 

            struct Inputvrite 
            { 
                float4 vertex : POSITION; 
                float4 texcoord : TEXCOORD0; 
            }; 
            struct Inputfragment 
            { 
                float4 pos : SV_POSITION; 
                float4 uv : TEXCOORD0; 
            }; 

            Inputfragment vertext_convert(Inputvrite i) 
            { 
                Inputfragment o; 
                o.pos = UnityObjectToClipPos(i.vertex); 
                o.uv = float4(i.texcoord.xy,1,1); 
                return o; 
            } 

            float4 fragment_convert(Inputfragment o) : COLOR 
            { 
                float4 c = tex2D(_MainTex,o.uv); 
                fixed4 alphaTex = tex2D(_Mask, o.uv);
                c.a = alphaTex.r; 
                return c; 
            } 

        ENDCG 
        } 
    } 
    FallBack "Diffuse" 
}